const express = require('express');
const cors = require('cors');
const fileUpload = require('express-fileupload');
const { specs, swaggerUi } = require('./config/swagger'); // 添加Swagger导入
require('dotenv').config();

const app = express();
const PORT = process.env.PORT || 3003;

// Debug: Print JWT_SECRET
console.log('JWT_SECRET from env:', process.env.JWT_SECRET);

// Middleware
app.use(cors());
app.use(express.json());
app.use(fileUpload());

// Swagger文档路由
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));

// Routes
app.get('/', (req, res) => {
  res.json({ message: 'FinanceApp API Server' });
});

// API Routes
const userRoutes = require('./routes/users');
app.use('/api/users', userRoutes);

const billRoutes = require('./routes/bills');
app.use('/api/bills', billRoutes);

const categoryRoutes = require('./routes/categories');
app.use('/api/categories', categoryRoutes);

const budgetRoutes = require('./routes/budgets');
app.use('/api/budgets', budgetRoutes);

const accountRoutes = require('./routes/accounts');
app.use('/api/accounts', accountRoutes);

const reportRoutes = require('./routes/reports');
app.use('/api/reports', reportRoutes);

const aiClassificationRoutes = require('./routes/ai-classification');
app.use('/api/ai', aiClassificationRoutes);

const importRoutes = require('./routes/import');
app.use('/api/import', importRoutes);

const backupRoutes = require('./routes/backup');
app.use('/api/backup', backupRoutes);

// Start server
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
  console.log(`Swagger文档地址: http://localhost:${PORT}/api-docs`);
});